package TableAdditions;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import Utility.CustomRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import Utility.BillLine;
import Utility.CustomCheckBox;
import Utility.CustomComboBox;
import Utility.Customer;
import Utility.Product;
import Utility.Promotion;
import Utility.JTextFieldFilter;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;


public class FillBillAction {
	
	Boolean personnelFlag=false;
	Boolean dayFlag=false;
	
	String selectedPersonnel="",selectedDate="";
	String selectedPersonelId="";
	String selectedDay="";
	String selectedProduct="";
	String selectedProductId="";
	public static String specialRouteId="";
	static int selectedCustomerId;
	public String routeId="";
	static int amount;
	static int promotionAmount;
	int customerCounter=0;
	static int tempIndex;
	static JLabel[] customerLabelArrays;
	static CustomRadioButton[] editRadioButtons;
	static JPanel[] editPanels;
	static JPanel[] pricePanels;
	static JTextField[] priceTextFields;
	static JTextField[] amountTextFields;
	static JTextField[] totalPromotionTextFields;
	static JTextField[] promotionAmountTextFields;
	static JButton[] saveButtons;
	static JButton[] editButtons;
	static JButton[] deleteButtons;
	static JButton[] plusButtons;
	static JButton[] minusButtons;
	
	
	static CustomComboBox[] productComboBoxs;
	
	static CustomCheckBox[] creditCheckBoxs;
	static CustomCheckBox[] cashCheckBoxs;
	static JTextField[] sumTextFields;
	static JTextField[] creditCashTextFields;
	public static BillLine[] billLines;
	static JPanel[] payingPanels;
	static int selectedLine;
	static float specialPrice ;
	static Boolean cashCheckBoxIsClicked=false;
	static Boolean creditCheckBoxIsClicked=false;
	static Boolean mouseClicked=false;
	static float total=0;
	static float totalCredit=0;
	static float totalCash=0;
	static int index=0;
	static int totalPromotion=0;
	static int tempTotalPromotion=0;
	static ButtonGroup radioButtonGroup = new ButtonGroup();
	public static int selectedRadioButton=0;
	public static int exSelectedRadioButton=0;
	public static int tempRowCounter=0;
	static Promotion[] promotions;
	public static Product[] products;
	public static Customer[] customers;
	public static boolean deleteFlag=true;
	
	
	public static void fillBillPage(final int rowCounter,JScrollPane jsp,final JPanel contentPane,final Product[] productArray,final Customer[] customerArray,String[] destinationsInRoute,Promotion[] promotionArray){
		tempRowCounter=0;
		tempRowCounter=rowCounter;
		//////System.out.println("in fill bill page");
		//jsp.repaint();
		
		products=productArray;
		customers=customerArray;
		promotions=promotionArray;
		contentPane.repaint();
		//selectedRadioButton=exSelectedRadioButton;
		billLines = new BillLine[rowCounter];
		//productComboBoxs = new CustomComboBox[rowCounter];
 	    productComboBoxs = new CustomComboBox[rowCounter];
     	customerLabelArrays = new JLabel[rowCounter];
     	editRadioButtons = new CustomRadioButton[rowCounter];
     	GridBagConstraints c = new GridBagConstraints();
        priceTextFields = new JTextField[rowCounter];
        amountTextFields = new JTextField[rowCounter];
        totalPromotionTextFields = new JTextField[rowCounter];
        promotionAmountTextFields = new JTextField[rowCounter];
        pricePanels= new JPanel[rowCounter];  
        editPanels = new JPanel[rowCounter];
        creditCheckBoxs=new CustomCheckBox[rowCounter];
     	cashCheckBoxs=new CustomCheckBox[rowCounter];
     	sumTextFields=new JTextField[rowCounter];
     	creditCashTextFields=new JTextField[rowCounter];
     	payingPanels=new JPanel[rowCounter];
     	saveButtons= new JButton[rowCounter];
     	editButtons= new JButton[rowCounter];
     	deleteButtons= new JButton[rowCounter];
     	plusButtons= new JButton[rowCounter];
     	minusButtons= new JButton[rowCounter];
     	
     	BillAdditionMenu.customerPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
     	
          for(int i=0;i<rowCounter;i++){ 
        	
        	////////////////////////System.out.println("in exception loop "+i);
          	c.fill = GridBagConstraints.HORIZONTAL;
          	//c.fill=GridBagConstraints.EAST;
          	////////////////////////System.out.println("customerLabel Added");
          	
          	final int tempI=i;
          	
          	editPanels[i]=new JPanel();
          	editPanels[i].setLayout(new GridLayout(1,4));
          	editRadioButtons[i]=new CustomRadioButton();
          	editRadioButtons[i].setCustomRadioButtonIndex(i);
          	
          	
          	radioButtonGroup.add(editRadioButtons[i]); 	
          	
          	
          	
          	
          	
          	editRadioButtons[i].addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					for(int j=0;j<tempRowCounter;j++){
						billLines[j].getBillProductComboBox().setEnabled(false);
					}
					
					exSelectedRadioButton=selectedRadioButton;
					////////////////////System.out.println("ex radioButton="+exSelectedRadioButton);
					
					
					
					for(int counter=0;counter<tempRowCounter;counter++){
						billLines[counter].getSaveButton().setEnabled(false);
						billLines[counter].getEditButton().setEnabled(false);
						billLines[counter].getDeleteButton().setEnabled(false);
						billLines[counter].getPlusButton().setEnabled(false);
						billLines[counter].getMinusButton().setEnabled(false);
						
					}
					
					/*billLines[exSelectedRadioButton].getCashCheckBox().setEnabled(false);
					billLines[exSelectedRadioButton].getCreditCheckBox().setEnabled(false);
					billLines[exSelectedRadioButton].getCreditCashTextField().setEnabled(false);
					billLines[exSelectedRadioButton].getCreditCashTextField().removeMouseListener(CustomMouseListener);*/
					selectedRadioButton=editRadioButtons[tempI].getCustomRadioButtonIndex();
					enableLine();
					////////////////////System.out.println("selectedradioButton="+selectedRadioButton);
					//////////////////////System.out.println("new radioButton="+selectedRadioButton);
					billLines[selectedRadioButton].getSaveButton().setEnabled(true);
					billLines[selectedRadioButton].getEditButton().setEnabled(true);
					billLines[selectedRadioButton].getDeleteButton().setEnabled(true);
					billLines[selectedRadioButton].getPlusButton().setEnabled(true);
					billLines[selectedRadioButton].getMinusButton().setEnabled(true);
					
					/*billLines[selectedRadioButton].getCashCheckBox().setEnabled(true);
					billLines[selectedRadioButton].getCreditCheckBox().setEnabled(true);
					billLines[selectedRadioButton].getCreditCashTextField().setEnabled(true);
					billLines[selectedRadioButton].getCreditCashTextField().addMouseListener(CustomMouseListener);*/
					
					
					
					billLines[selectedRadioButton].getBillProductComboBox().setEnabled(true);
					enableLine();
					
					

				}
			});
          	
          	
          	editPanels[i].add(editRadioButtons[i]);
          	
          	
          	
          	
          	plusButtons[i]=new JButton(new javax.swing.ImageIcon(FillBillAction.class.getResource("plus.png")));
			plusButtons[i].setBorder(BorderFactory.createEmptyBorder());
			plusButtons[i].setContentAreaFilled(false);
			plusButtons[i].setEnabled(false);
			editPanels[i].add(plusButtons[i]);
			
			
			
			
			plusButtons[i].addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					specialRouteId=BillAdditionMenu.selectedPersonnel+"/"+BillAdditionMenu.billdate;
					////////System.out.println("specialRouteId="+specialRouteId);
					
					
					Connection con = null;
			        Statement st = null;
			        ResultSet rs = null;

			        String url = "jdbc:mysql://localhost:3306/mydb";
			        String user = "root";
			        String password = "qweasdzxc";
			        
			        
			        try {
						con = (Connection) DriverManager.getConnection(url, user, password);
						st = (Statement) con.createStatement();
						
						
						   
			            rs=st.executeQuery("SELECT * FROM special_route WHERE route_id='"+specialRouteId+"'");
			            
			            int specialRouteCounter=0;
			            
			            while(rs.next()){
			            	specialRouteCounter++;
			            }
			            
			            ////////System.out.println("specialRouteCounter="+specialRouteCounter);
			            
			            if(specialRouteCounter==0){
			            	////////System.out.println("special route adding....");
			            	if(BillAdditionMenu.personnelFlag && BillAdditionMenu.dayFlag){
			            		st.executeUpdate("INSERT INTO special_route (route_id,route_owner_id,route_date)" +
			            				" VALUES ('"+specialRouteId+"','"+BillAdditionMenu.selectedPersonelId+"','"+BillAdditionMenu.billdate+"')");
			            		
			            		////////System.out.println("destination number to add !!!!!!!!!!! ="+BillAdditionMenu.destinationsInRoute.length);
			            		for(int x=0; x<BillAdditionMenu.destinationsInRoute.length;x++){
			            			st.executeUpdate("INSERT INTO special_route_destination_list (route_id,destination_id)" +
				            				" VALUES ('"+specialRouteId+"','"+BillAdditionMenu.destinationsInRoute[x]+"')");
			            			
			            		}
			            		
			            	}
			            	
			            	
			            }
			           
			            st.executeUpdate("INSERT INTO special_route_destination_list (route_id,destination_id) " + "VALUES ('"+specialRouteId+"'" +
			            		",'"+billLines[selectedRadioButton].getCustomerId()+"')");
			            deleteFlag=false;
			            selectedRadioButton=selectedRadioButton+1;
			            BillAdditionMenu.refreshDestinationsInRoute();
						
						
						con.close();
						
						
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		            

					
					
					
					
				}
				
				
				
				
				
				});
			
			
			
			
			
			
			
			minusButtons[i]=new JButton(new javax.swing.ImageIcon(FillBillAction.class.getResource("minus.png")));
			minusButtons[i].setBorder(BorderFactory.createEmptyBorder());
			minusButtons[i].setContentAreaFilled(false);
			minusButtons[i].setEnabled(false);
			editPanels[i].add(minusButtons[i]);
			
			
          	
			minusButtons[i].addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					
					Statement stmt = null;
                	Connection conn = null;
                	ResultSet rss = null;
                	String url = "jdbc:mysql://localhost:3306/mydb";
        	        String user = "root";
        	        String password = "qweasdzxc";
        	        
        	        
        	        try {
						conn = (Connection) DriverManager.getConnection(url, user, password);
						stmt = (Statement) conn.createStatement();
						int temp11=1;
						rss=stmt.executeQuery("SELECT * FROM special_route_destination_list WHERE destination_id='"+billLines[selectedRadioButton].getCustomerId()+"' and route_id='"+BillAdditionMenu.routeId+"'");
						
						int duplicateCounter =0;
						
						while(rss.next()){
							duplicateCounter++;
						}
						
						//System.out.println("duplicateCounter!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!="+duplicateCounter);
						if(duplicateCounter>1){
							////System.out.println("specialRouteId="+BillAdditionMenu.routeId);
							stmt.executeUpdate("DELETE FROM special_route_destination_list WHERE destination_id='"+billLines[selectedRadioButton].getCustomerId()+"' and route_id='"+BillAdditionMenu.routeId+"'");
							for(int b=0;b<duplicateCounter-1;b++){
								stmt.executeUpdate("INSERT INTO special_route_destination_list (route_id,destination_id)" +
			            				" VALUES ('"+BillAdditionMenu.routeId+"','"+billLines[selectedRadioButton].getCustomerId()+"')");
							}
						}
						deleteFlag=false;
						if(selectedRadioButton>0){
							selectedRadioButton=selectedRadioButton-1;
						}
						BillAdditionMenu.refreshDestinationsInRoute();
						
						conn.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
					
				}
			});
          	
          	
          	
          	
          	
          	//addItem(BillAdditionMenu.customerPanel,editRadioButtons[i], 0,i,0, 1, GridBagConstraints.WEST,20);
          	
          	//BufferedImage buttonIcon = ImageIO.read(new File("images/savebutton.gif"));
			saveButtons[i]=new JButton(new javax.swing.ImageIcon(FillBillAction.class.getResource("savebutton.gif")));
			//saveButtons[i].setPreferredSize(new Dimension(10,10));
			saveButtons[i].setBorder(BorderFactory.createEmptyBorder());
			saveButtons[i].setContentAreaFilled(false);
			saveButtons[i].setEnabled(false);
			
			editPanels[i].add(saveButtons[i]);
			
			
			saveButtons[i].addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					//////////////////////System.out.println("ready to add line "+selectedRadioButton);
					
					
					
					Statement stmt = null;
			    	Connection conn = null;
			    	ResultSet rss = null;
			    	String url = "jdbc:mysql://localhost:3306/mydb";
			        String user = "root";
			        String password = "qweasdzxc";
			        
					 try {
						    conn = (Connection) DriverManager.getConnection(url, user, password);
							stmt = (Statement) conn.createStatement();
							
						
						    
							BillAdditionMenu.billdate=BillAdditionMenu.getDate();
							
						    //////////////////////////System.out.println("date ="+date+"   datapicker date="+datePickerDate);
						    
						    //add date to all lines
						    for(int m=0;m<billLines.length;m++){
						    	billLines[m].setBillDate(BillAdditionMenu.billdate);
						    }
						    
						    //add personnelId to lines
						    for(int m=0;m<billLines.length;m++){
						    	billLines[m].setPersonnelId(Integer.parseInt(BillAdditionMenu.selectedPersonelId));
						    }
						    
						    
						    // controls for preventing data repetition
						    
						    rss=stmt.executeQuery("SELECT * from bill");
						    
						    while(rss.next()){
						    		for(int m=0;m<billLines.length;m++){
						    			//////////////////////////System.out.println("database cCode: "+rss.getString("customer_code")+"  "+"line cCode: "+billLines[selectedRadioButton].getCustomerCode());
						    			//////////////////////////System.out.println("database date: "+rss.getString("bill_date")+"  "+"line date: "+billLines[selectedRadioButton].getBillDate());
						    			//////////////////////////System.out.println("database proid: "+rss.getInt("product_id")+"  "+"line proid: "+billLines[selectedRadioButton].getProductId());
						    			if(rss.getString("customer_code").equals(billLines[m].getCustomerCode())
						    					&& rss.getString("bill_date").equals(billLines[m].getBillDate())
						    					&& rss.getInt("product_id")==billLines[m].getProductId()
						    					&& rss.getInt("personel_id")==billLines[m].getPersonnelId()){
						    				billLines[m].setBillSuitability(false);
						    				//////////////////////////System.out.println("Duplicate date found");
						    			}
						    		}
						    }
						    
						    
						    
						    
							
							
								
								
								if(billLines[selectedRadioButton].getBillSuitability()){
								
								if(billLines[selectedRadioButton].getCashCheckBoxStatus()){
									
									
										////////System.out.println("CustomerCode to add= "+billLines[selectedRadioButton].getCustomerCode());
										stmt.executeUpdate("INSERT INTO bill (customer_code,personel_id,bill_cost,bill_date,product_id,bill_cash,bill_amount,bill_credit,special_price,promotion_amount) " + 
												"VALUES ('"+billLines[selectedRadioButton].getCustomerCode()+"'" +
														",'"+BillAdditionMenu.selectedPersonelId+"','"+billLines[selectedRadioButton].getBillTotalCost()+"','"+BillAdditionMenu.billdate+"'" +
			    			            					",'"+billLines[selectedRadioButton].getProductId()+"','"+billLines[selectedRadioButton].getBillTotalCost()+"'," +
			    			            							"'"+billLines[selectedRadioButton].getAmount()+"','"+0+"','"+billLines[selectedRadioButton].getSpecialPrice()+"','"+billLines[selectedRadioButton].getBillPromotionAmount()+"')");
												
												if(billLines[selectedRadioButton].getBillPromotionAmount()>0){
													//////////////////////////System.out.println("Promotion updated!!!!!!!!!!!!!!!!!!!!!!!!!!");
													//////////////////////////System.out.println("promotion id= "+billLines[selectedRadioButton].getBillPromotion().getPromotionId());
													//////////////////////////System.out.println("unit="+billLines[selectedRadioButton].getBillPromotionUnit()+" amount="+billLines[selectedRadioButton].getBillPromotionAmount());
													//////////////////System.out.println("totalpromotionUnit before update promotion="+billLines[selectedRadioButton].getBillPromotionUnit());
													//////////////////System.out.println("bill promotion amount="+billLines[selectedRadioButton].getBillPromotionAmount());
													int promotionUnit=billLines[selectedRadioButton].getBillPromotionUnit()-billLines[selectedRadioButton].getBillPromotionAmount();
													//////////////////System.out.println("new promotionUnit="+promotionUnit);
													billLines[selectedRadioButton].setBillPromotionUnit(promotionUnit);
													stmt.executeUpdate("UPDATE promotion SET promotion_unit='"+promotionUnit+"' WHERE promotion_id='"+billLines[selectedRadioButton].getBillPromotion().getPromotionId()+"'");
												
													
													UpdatePromotions();
										          //	fillBillSpecialPage();
													BillAdditionMenu.refreshDestinationsInRoute();
													 
													
												
												}
												
												
									
									
			        			}
								
								if(billLines[selectedRadioButton].getCreditCheckBoxStatus()){
									
									////////System.out.println("CustomerCode to add= "+billLines[selectedRadioButton].getCustomerCode());
											stmt.executeUpdate("INSERT INTO bill (customer_code,personel_id,bill_cost,bill_date,product_id,bill_cash,bill_amount,bill_credit,special_price,promotion_amount) " + 
											"VALUES ('"+billLines[selectedRadioButton].getCustomerCode()+"'" +
													",'"+BillAdditionMenu.selectedPersonelId+"','"+billLines[selectedRadioButton].getBillTotalCost()+"','"+BillAdditionMenu.billdate+"'" +
						            					",'"+billLines[selectedRadioButton].getProductId()+"','"+0+"'," +
						            							"'"+billLines[selectedRadioButton].getAmount()+"','"+billLines[selectedRadioButton].getBillTotalCost()+"','"+billLines[selectedRadioButton].getSpecialPrice()+"'" +
						            									",'"+billLines[selectedRadioButton].getBillPromotionAmount()+"')");
											
											if(billLines[selectedRadioButton].getBillPromotionAmount()>0){
												//////////////////////////System.out.println("Promotion updated!!!!!!!!!!!!!!!!!!!!!!!!!!");
												//////////////////////////System.out.println("promotion id= "+billLines[selectedRadioButton].getBillPromotion().getPromotionId());
												//////////////////////////System.out.println("unit="+billLines[selectedRadioButton].getBillPromotionUnit()+" amount="+billLines[selectedRadioButton].getBillPromotionAmount());
												//////////////////System.out.println("totalpromotionUnit before update promotion="+billLines[selectedRadioButton].getBillPromotionUnit());
												//////////////////System.out.println("bill promotion amount="+billLines[selectedRadioButton].getBillPromotionAmount());
												int promotionUnit=billLines[selectedRadioButton].getBillPromotionUnit()-billLines[selectedRadioButton].getBillPromotionAmount();
												//////////////////System.out.println("new promotionUnit"+promotionUnit);
												billLines[selectedRadioButton].setBillPromotionUnit(promotionUnit);
												stmt.executeUpdate("UPDATE promotion SET promotion_unit='"+promotionUnit+"' WHERE promotion_id='"+billLines[selectedRadioButton].getBillPromotion().getPromotionId()+"'");
			            				
												UpdatePromotions();
												//fillBillSpecialPage();
												BillAdditionMenu.refreshDestinationsInRoute();
									          											 
										
											
											}
											
											
									
			        			}
								
								if(billLines[selectedRadioButton].getCreditCashTextFieldStatus()){
									
											////////System.out.println("CustomerCode to add= "+billLines[selectedRadioButton].getCustomerCode());
											Float payedCash=billLines[selectedRadioButton].getPayedCash();
											Float totalc = billLines[selectedRadioButton].getBillTotalCost();
											float remainingCredit= totalc-payedCash;
											
											
											stmt.executeUpdate("INSERT INTO bill (customer_code,personel_id,bill_cost,bill_date,product_id,bill_cash,bill_amount,bill_credit,special_price,promotion_amount) " + 
											"VALUES ('"+billLines[selectedRadioButton].getCustomerCode()+"'" +
													",'"+BillAdditionMenu.selectedPersonelId+"','"+billLines[selectedRadioButton].getBillTotalCost()+"','"+BillAdditionMenu.billdate+"'" +
						            					",'"+billLines[selectedRadioButton].getProductId()+"','"+billLines[selectedRadioButton].getPayedCash()+"'," +
						            							"'"+billLines[selectedRadioButton].getAmount()+"','"+remainingCredit+"','"+billLines[selectedRadioButton].getSpecialPrice()+"'" +
						            									",'"+billLines[selectedRadioButton].getBillPromotionAmount()+"')");
											
											if(billLines[selectedRadioButton].getBillPromotionAmount()>0){
												//////////////////////////System.out.println("Promotion updated!!!!!!!!!!!!!!!!!!!!!!!!!!");
												//////////////////////////System.out.println("promotion id= "+billLines[selectedRadioButton].getBillPromotion().getPromotionId());
												//////////////////////////System.out.println("unit="+billLines[selectedRadioButton].getBillPromotionUnit()+" amount="+billLines[selectedRadioButton].getBillPromotionAmount());
												//////////////////System.out.println("totalpromotionUnit before update promotion="+billLines[selectedRadioButton].getBillPromotionUnit());
												//////////////////System.out.println("bill promotion amount="+billLines[selectedRadioButton].getBillPromotionAmount());
												int promotionUnit=billLines[selectedRadioButton].getBillPromotionUnit()-billLines[selectedRadioButton].getBillPromotionAmount();
												//////////////////System.out.println("new promotionUnit"+promotionUnit);
												billLines[selectedRadioButton].setBillPromotionUnit(promotionUnit);
												stmt.executeUpdate("UPDATE promotion SET promotion_unit='"+promotionUnit+"' WHERE promotion_id='"+billLines[selectedRadioButton].getBillPromotion().getPromotionId()+"'");
												
												UpdatePromotions();
									          	//fillBillSpecialPage();
												BillAdditionMenu.refreshDestinationsInRoute();
									          											
												
			            				
											
											}
											
											
									
			        			}
								
								
								
							}
								
								int messageType = JOptionPane.PLAIN_MESSAGE;
								JOptionPane.showMessageDialog(null, "Kayıt Alındı", "Uyarı!!", messageType);
								UpdatePromotions();
					          	fillBillSpecialPage();
					          							 
								
							conn.close();
							// stmt.executeUpdate("INSERT INTO job (job_name) " + "VALUES ('"+name_field.getText()+"')");
						} catch (SQLException er) {
							// TODO Auto-generated catch block
							er.printStackTrace();
						}
			    	
			    	
					
						
				}
			});
			
			
			//BufferedImage image2 = ImageIO.read(FillBillAction.class.getClassLoader().getResource("images/editBig.gif"));
			//BufferedImage buttonIcon2 = ImageIO.read(new File("images/editBig.gif"));
			editButtons[i]=new JButton(new javax.swing.ImageIcon(FillBillAction.class.getResource("editBig.gif")));
			editButtons[i].setBorder(BorderFactory.createEmptyBorder());
			editButtons[i].setContentAreaFilled(false);
			editButtons[i].setEnabled(false);
			
			editPanels[i].add(editButtons[i]);
			
			editButtons[i].addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					
					CalculateTotalCostOfLine();
					Calculate(rowCounter);
					

					Statement stmt = null;
			    	Connection conn = null;
			    	ResultSet rss = null;
			    	String url = "jdbc:mysql://localhost:3306/mydb";
			        String user = "root";
			        String password = "qweasdzxc";
			        
					 try {
						    conn = (Connection) DriverManager.getConnection(url, user, password);
							stmt = (Statement) conn.createStatement();
							
						
						    
							BillAdditionMenu.billdate=BillAdditionMenu.getDate();
							
						    //////////////////////////System.out.println("date ="+date+"   datapicker date="+datePickerDate);
						    
						    //add date to all lines
						    for(int m=0;m<billLines.length;m++){
						    	billLines[m].setBillDate(BillAdditionMenu.billdate);
						    }
						    
						    //add personnelId to lines
						    for(int m=0;m<billLines.length;m++){
						    	billLines[m].setPersonnelId(Integer.parseInt(BillAdditionMenu.selectedPersonelId));
						    }
						    
						    
						    // controls for preventing data repetition
						    
						 
						    
						    
						    
						    
							
							
								
						    			if(billLines[selectedRadioButton].getBillModifyingPromotionAmount()==0){
						    				billLines[selectedRadioButton].setBillModifyingPromotionAmount(billLines[selectedRadioButton].getBillPromotionAmount());
						    			}
					 
								
										float credit=billLines[selectedRadioButton].getBillTotalCost()-billLines[selectedRadioButton].getPayedCash();
										if(credit<0){
											credit=0;
										}
										
										float billCash = billLines[selectedRadioButton].getBillTotalCost()-credit;
										
										if(billLines[selectedRadioButton].getBillTotalCost()<billLines[selectedRadioButton].getPayedCash()){
											billLines[selectedRadioButton].setPayedCash(billLines[selectedRadioButton].getBillTotalCost());
										}
										
										if(billLines[selectedRadioButton].getCreditCashTextField().getText().equals("") || billLines[selectedRadioButton].getCreditCashTextField().getText().equals("0.0")){
											billLines[selectedRadioButton].setPayedCash(0);
										}
										
										//////////////System.out.println("new totalCost= "+billLines[selectedRadioButton].getBillTotalCost()+" credit="+credit
											//	+"cash="+billLines[selectedRadioButton].getBillCash());
										
										
										//////System.out.println("new product_id="+billLines[selectedRadioButton].getProductId());
										
										if(!billLines[selectedRadioButton].getAmountTextField().getText().equals("")){
											billLines[selectedRadioButton].setAmount(Integer.parseInt(billLines[selectedRadioButton].getAmountTextField().getText()))	;
										}
										
										//////System.out.println("new amount="+billLines[selectedRadioButton].getAmount());
										
										stmt.executeUpdate("UPDATE bill SET bill_cost='"+billLines[selectedRadioButton].getBillTotalCost()+"',product_id='"+billLines[selectedRadioButton].getProductId()+"'" +
												",bill_cash='"+billCash+"',bill_amount='"+billLines[selectedRadioButton].getAmount()+"'" +
														",bill_credit='"+credit+"',special_price='"+billLines[selectedRadioButton].getSpecialPrice()+"',promotion_amount='"+billLines[selectedRadioButton].getBillModifyingPromotionAmount()+"'" +
																"WHERE bill_id='"+billLines[selectedRadioButton].getBillId()+"'");  
												
												
													int newUnit=billLines[selectedRadioButton].getBillModifyingPromotionUnit();
													//////////////System.out.println("new promotion unit before update="+newUnit);
													stmt.executeUpdate("UPDATE promotion SET promotion_unit='"+newUnit+"' WHERE promotion_id='"+billLines[selectedRadioButton].getBillPromotion().getPromotionId()+"'");
													UpdatePromotions();
										          	fillBillSpecialPage();
										          											          
													 
													 int messageType = JOptionPane.PLAIN_MESSAGE;
													 JOptionPane.showMessageDialog(null, "Kayıt Güncellendi", "Uyarı!!", messageType);
												
												
													 CalculateTotalCostOfLine();
														Calculate(rowCounter);
							
								
													UpdatePromotions();
													fillBillSpecialPage();
					          		
					          	
								conn.close();
							
							// stmt.executeUpdate("INSERT INTO job (job_name) " + "VALUES ('"+name_field.getText()+"')");
						} catch (SQLException er) {
							// TODO Auto-generated catch block
							er.printStackTrace();
						}
			    	
			    	
				}
			});
			
			
			//BufferedImage image3 = ImageIO.read(FillBillAction.class.getClassLoader().getResource("images/delete.jpg"));
			//BufferedImage buttonIcon3 = ImageIO.read(new File("images/delete.jpg"));
			deleteButtons[i]=new JButton(new javax.swing.ImageIcon(FillBillAction.class.getResource("delete.jpg")));
			deleteButtons[i].setBorder(BorderFactory.createEmptyBorder());
			deleteButtons[i].setContentAreaFilled(false);
			deleteButtons[i].setEnabled(false);
			
			editPanels[i].add(deleteButtons[i]);
			
			deleteButtons[i].addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					
					
					Statement stmt = null;
			    	Connection conn = null;
			    	ResultSet rss = null;
			    	String url = "jdbc:mysql://localhost:3306/mydb";
			        String user = "root";
			        String password = "qweasdzxc";
			        
					 try {
						    conn = (Connection) DriverManager.getConnection(url, user, password);
							stmt = (Statement) conn.createStatement();
							
							
						    
							BillAdditionMenu.billdate=BillAdditionMenu.getDate();
							
						    //////////////////////////System.out.println("date ="+date+"   datapicker date="+datePickerDate);
						    
						    //add date to all lines
						    for(int m=0;m<billLines.length;m++){
						    	billLines[m].setBillDate(BillAdditionMenu.billdate);
						    }
						    
						    //add personnelId to lines
						    for(int m=0;m<billLines.length;m++){
						    	billLines[m].setPersonnelId(Integer.parseInt(BillAdditionMenu.selectedPersonelId));
						    }
						    
						    
						    // controls for preventing data repetition
						    
						    rss=stmt.executeQuery("SELECT * from bill");
						    
						    while(rss.next()){
						    		for(int m=0;m<billLines.length;m++){
						    			//////////////////////////System.out.println("database cCode: "+rss.getString("customer_code")+"  "+"line cCode: "+billLines[selectedRadioButton].getCustomerCode());
						    			//////////////////////////System.out.println("database date: "+rss.getString("bill_date")+"  "+"line date: "+billLines[selectedRadioButton].getBillDate());
						    			//////////////////////////System.out.println("database proid: "+rss.getInt("product_id")+"  "+"line proid: "+billLines[selectedRadioButton].getProductId());
						    			if(rss.getString("customer_code").equals(billLines[m].getCustomerCode())
						    					&& rss.getString("bill_date").equals(billLines[m].getBillDate())
						    					&& rss.getInt("product_id")==billLines[m].getProductId()
						    					&& rss.getInt("personel_id")==billLines[m].getPersonnelId()){
						    				billLines[m].setBillSuitability(false);
						    				//////////////////////////System.out.println("Duplicate date found");
						    			}
						    		}
						    }
						    
						    
						    
						
										
										
										
										stmt.executeUpdate("DELETE FROM bill WHERE bill_id='"+billLines[selectedRadioButton].getBillId()+"'");
										//billLines[selectedRadioButton].setBillPromotionUnit(billLines[selectedRadioButton].getBillModifyingPromotionUnit());
										int newUnit=billLines[selectedRadioButton].getBillModifyingPromotionUnit()+billLines[selectedRadioButton].getBillPromotionAmount();
										stmt.executeUpdate("UPDATE promotion SET promotion_unit='"+newUnit+"' WHERE promotion_id='"+billLines[selectedRadioButton].getBillPromotion().getPromotionId()+"'");
										
										
										
										int messageType = JOptionPane.PLAIN_MESSAGE;
										JOptionPane.showMessageDialog(null, "Kayıt Silindi", "Uyarı!!", messageType);
										
										billLines[selectedRadioButton].setBillSuitability(true);
										deleteFlag=false;
										UpdatePromotions();
							          	//fillBillSpecialPage();
										BillAdditionMenu.refreshDestinationsInRoute();
							          								          
							conn.close();
							// stmt.executeUpdate("INSERT INTO job (job_name) " + "VALUES ('"+name_field.getText()+"')");
						} catch (SQLException er) {
							// TODO Auto-generated catch block
							er.printStackTrace();
						}
			    	
			    	
					
					
				}
			});
          	
          	
          	
          	
          	
			
			
          	
          	
          	
          	
          	
          	int customerIndex=Integer.parseInt(destinationsInRoute[i]);
          	////////////////////////System.out.println("customerIndex= "+customerIndex);
          	int j=0;
          	int customerFinder=-1;
          	for(j=0;j<customerArray.length;j++){
          		////////////////////////System.out.println("Customer Name = "+customerArray[j].getCustomerName());
          		if(customerIndex==customerArray[j].getCustomerId()){
          			customerFinder=j;
          		}
          	}
          	
          	billLines[i]=new BillLine(i);
          	
          	billLines[i].setSaveButton(saveButtons[i]);
        	billLines[i].setEditButton(editButtons[i]);
        	billLines[i].setDeleteButton(deleteButtons[i]);
        	billLines[i].setPlusButton(plusButtons[i]);
        	billLines[i].setMinusButton(minusButtons[i]);
        	
          	
          	customerLabelArrays[i]=new JLabel(customerArray[customerFinder].getCustomerName());
          	billLines[i].setCustomerCode(customerArray[customerFinder].getCustomerCode());
          	billLines[i].setCustomerId(customerArray[customerFinder].getCustomerId());
          	customerLabelArrays[i].setBorder(BorderFactory.createLineBorder(Color.GRAY));
          	tempIndex=i;
          	billLines[i].setCustomerLabel(customerLabelArrays[i]);
          	
          	addItem(BillAdditionMenu.customerPanel, editPanels[i],0, i,1, 1, GridBagConstraints.WEST,20);
          	addItem(BillAdditionMenu.customerPanel, billLines[i].getCustomerLabel(), 1, i, 1, 1, GridBagConstraints.WEST,20);
          	//editPanels[i].add(billLines[i].getCustomerLabel());
          	
          	
          	productComboBoxs[i]=new CustomComboBox(i);
          	productComboBoxs[i].addItem("Ürün Adı");
          	for(int m=0;m<productArray.length;m++){
          		////////////////////////System.out.println("product Added");
          		//productComboBoxs[i].addItem(productArray[m].GetLongProductName());
          		productComboBoxs[i].addItem(productArray[m].getProductName());
          	}
          	//final int tempI1=i;
          	
          	
          	productComboBoxs[i].addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent event) {
							totalPromotion=0;
							index=0;
							//////////////////////System.out.println("in productComboBox'''''''''''''''''''''''''''''''''''''''");
							////////////////////////System.out.println("ex selectedLine= "+selectedLine);
							//billLines[selectedLine].setBillPromotionUnit(totalPromotion);
							////////////////////////System.out.println("ex totalPromotion="+totalPromotion);
							////////////////////////System.out.println("ex line promotionUnit="+billLines[selectedLine].getBillPromotionUnit());
							////////////////////////System.out.println("ex line promotionAmount="+billLines[selectedLine].getBillPromotionAmount());
						//billLines[selectedLine].setBillPromotionAmount(promotionAmount);
						
						
						
						for(int j=0;j<rowCounter;j++){
							//billLines[j].getCreditCheckBox().setEnabled(false);
							//billLines[j].getCashCheckBox().setEnabled(false);
							//billLines[j].getCreditCashTextField().setEditable(false);
							//billLines[j].getCreditCashTextField().setEnabled(false);
							//billLines[j].getAmountTextField().setEditable(false);
							//billLines[j].getPromotionTextField().setEditable(false);
							billLines[j].getCreditCashTextField().removeMouseListener(CustomMouseListener);
						}  
						
						cashCheckBoxIsClicked=false;
						creditCheckBoxIsClicked=false;
						mouseClicked=false;
						
						selectedLine=productComboBoxs[selectedRadioButton].GetId();
						
						totalPromotionTextFields[selectedLine].setText("");
						creditCashTextFields[selectedLine].setText("");
						////////////////////////System.out.println("SelectedLine!!!!!!!!!= "+selectedLine);
						////////////////////////System.out.println("Customer= "+customerLabelArrays[selectedLine].getText());
						
	                	Statement stmt = null;
	                	Connection conn = null;
	                	ResultSet rss = null;
	                	String url = "jdbc:mysql://localhost:3306/mydb";
	        	        String user = "root";
	        	        String password = "qweasdzxc";
	        	        
	        	        try {
							conn = (Connection) DriverManager.getConnection(url, user, password);
							stmt = (Statement) conn.createStatement();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
	        	        
	        	        index=productComboBoxs[selectedRadioButton].getSelectedIndex();
	        	        
	        	   
	    	          
	                	
	                	
	                	
	                    JComboBox comboBox = (JComboBox) event.getSource();
	     
	                    //
	                    // Print the selected items and the action command.
	                    //
	                    Object selected = comboBox.getSelectedItem();
	                    ////////////////////////System.out.println("Selected Item  = " + selected);
	                    String command = event.getActionCommand();
	                    ////////////////////////System.out.println("Action Command = " + command);
	                    
	                    
	                    selectedCustomerId=DatabaseFunctions.getCustomerIdFromName(customerLabelArrays[selectedRadioButton].getText());
	                    billLines[selectedRadioButton].setCustomerId(selectedCustomerId);
	                   // ////////////////////System.out.println("selectedCustomerId="+selectedCustomerId);
	                    
	                    //////////////////////System.out.println("Index&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&6   = "+index);
	                    
	                   
	                    
	                    
	                   // //////////////////////System.out.println("SELECTEDLINE="+selectedLine);
	                    if(index>0){
	                    	int selectedProductId=DatabaseFunctions.getProductIdFromName(productArray[index-1].getProductName());
	                    	////////////////////System.out.println("selectedProductId!!!!!!!!!!!!!!!!!!!!!!!!!!!!!="+selectedProductId);
	                    	billLines[selectedRadioButton].setProductId(selectedProductId);
	                    	
	                    	specialPrice = DatabaseFunctions.getSpecialPrice(selectedCustomerId,selectedProductId);
	                    	System.out.println("specialPrice= !!!!!!!!!!!!!!!!!!!!!!!!!!   "+specialPrice);
	                    
	                    	priceTextFields[selectedRadioButton].setText(Float.toString(specialPrice));
                			//priceTextFields[selectedRadioButton].setText("333");
                			priceTextFields[selectedRadioButton].setEditable(false);
 	                    	//////////////////////System.out.println("selectedProductId="+selectedProductId);
 	                    	billLines[selectedRadioButton].setSpecialPrice(specialPrice);
 	                    	billLines[selectedRadioButton].getAmountTextField().setEditable(true);
 	                    	billLines[selectedRadioButton].getCreditCashTextField().setEditable(true);
 	                    	billLines[selectedRadioButton].getCreditCashTextField().addMouseListener(CustomMouseListener);
 	                    	billLines[selectedRadioButton].getCashCheckBox().setEnabled(true);
 	                    	billLines[selectedRadioButton].getCreditCheckBox().setEnabled(true);
 	                    	amountTextFields[selectedRadioButton].setEditable(true);
	 	                    amountTextFields[selectedRadioButton].setText("");
	 	                    sumTextFields[selectedRadioButton].setText("");
	                    	
	                  
	                    	UpdatePromotions();
	                    	for(int k=0;k<promotions.length;k++){
	                    		//////////////////////System.out.println("product id in bill= "+billLines[k].getProductId()+" product id in promotion= "+promotionArray[k].getPromotionProductId());
	                    		//////////////////////System.out.println("customer id in bill= "+billLines[k].getCustomerId()+" customer id in promotion= "+promotionArray[k].getPromotionCustomerId());
	                    		if(billLines[selectedRadioButton].getProductId()==promotions[k].getPromotionProductId() 
	                    				&& billLines[selectedRadioButton].getCustomerId()==promotions[k].getPromotionCustomerId()  ){
	                    			billLines[selectedRadioButton].setBillPromotion(promotions[k]);
	                    			//////////////////System.out.println("promotionId="+promotionArray[k].getPromotionId()+" and PromotionUnit="+promotionArray[k].getPromotionUnit());
	                    			billLines[selectedRadioButton].setBillPromotionUnit(promotions[k].getPromotionUnit());
	                    			billLines[selectedRadioButton].setBillModifyingPromotionAmount(0);
	                    			billLines[selectedRadioButton].setBillModifyingPromotionUnit(promotions[k].getPromotionUnit());
	                    			////////////////////////System.out.println("unit in bill line="+billLines[selectedLine].getBillPromotionUnit());
	                    			promotionAmountTextFields[selectedRadioButton].setText("");
	                    			totalPromotionTextFields[selectedRadioButton].setText(Integer.toString(billLines[selectedRadioButton].getBillPromotionUnit()));
	                    			////////////////////System.out.println("Promotion Found!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! and promotion unit="+billLines[selectedRadioButton].getBillPromotionUnit());
	                    			promotionAmountTextFields[selectedRadioButton].setEditable(true);
	                    			//////////////////System.out.println("Before Break!!!!!!!!!");
	                    			k=promotions.length;
	                    	}
	                    		else{
	                    			////////////////////////System.out.println("Promotion not found!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
	                    			promotionAmountTextFields[selectedRadioButton].setText("");
	                    			//totalPromotionTextFields[selectedLine].setText("0");
	                    			//promotionAmountTextFields[selectedLine].setText("0");
	                    			promotionAmountTextFields[selectedRadioButton].setEditable(false);
	                    		}
	                    		
	                    		
	                    		 if(index>0){
	                    			priceTextFields[selectedRadioButton].setText(Float.toString(specialPrice));
	                    			//priceTextFields[selectedRadioButton].setText("333");
	                    			priceTextFields[selectedRadioButton].setEditable(false);
	     	                    	//////////////////////System.out.println("selectedProductId="+selectedProductId);
	     	                    	billLines[selectedRadioButton].setSpecialPrice(specialPrice);
	     	                    	billLines[selectedRadioButton].getAmountTextField().setEditable(true);
	     	                    	billLines[selectedRadioButton].getCreditCashTextField().setEditable(true);
	     	                    	billLines[selectedRadioButton].getCreditCashTextField().addMouseListener(CustomMouseListener);
	     	                    	billLines[selectedRadioButton].getCashCheckBox().setEnabled(true);
	     	                    	billLines[selectedRadioButton].getCreditCheckBox().setEnabled(true);
	     	                    	amountTextFields[selectedRadioButton].setEditable(true);
	    	 	                    amountTextFields[selectedRadioButton].setText("");
	    	 	                    sumTextFields[selectedRadioButton].setText("");
	    	 	                    //totalPromotionTextFields[selectedRadioButton].setText("");
	     	                    }
	                    		 else if(index==0){
	                    			promotionAmountTextFields[selectedRadioButton].setText("");
	                    			totalPromotionTextFields[selectedRadioButton].setText("");
	                    			////////////////////////System.out.println("index 0000000000000000000000000000000");
	                    			promotionAmountTextFields[selectedRadioButton].setEditable(false);
	                    			billLines[selectedRadioButton].getAmountTextField().setEditable(false);
	     	                    	billLines[selectedRadioButton].getCreditCashTextField().setEditable(false);
	     	                    	billLines[selectedRadioButton].getCreditCashTextField().removeMouseListener(CustomMouseListener);
	     	                    	billLines[selectedRadioButton].getCashCheckBox().setEnabled(false);
	     	                    	billLines[selectedRadioButton].getCreditCheckBox().setEnabled(false);
	     	                    	priceTextFields[selectedRadioButton].setText("");
	     	                    	totalPromotionTextFields[selectedRadioButton].setText("");
	     	                    	amountTextFields[selectedRadioButton].setText("");
	     	                    	sumTextFields[selectedRadioButton].setText("");
	    	                    	priceTextFields[selectedRadioButton].setEditable(false);
	    	                    	amountTextFields[selectedRadioButton].setEditable(false);
	                    			
	                    			
	                    		}
	                    }
	                    	
	                    }
	                    	
	                    //////////////////////System.out.println("selectedline= "+selectedLine+" selectedLine= "+selectedLine);
	                    
	                    
	                   // //////////////////////System.out.println("specialPrice="+specialPrice);
	                    
	             
						
				
				} //////////////////////

					});

          	addItem(BillAdditionMenu.customerPanel, productComboBoxs[i], 2, i, 1, 1, GridBagConstraints.WEST,20);
          	
          	billLines[i].setBillProductComboBox(productComboBoxs[i]);
          	
          	
          	
          	
          	
          	
          	pricePanels[i]=new JPanel();
          	pricePanels[i].setLayout(new GridLayout(1,3));
          	
          	priceTextFields[i]=new JTextField(3);
          	priceTextFields[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
          	priceTextFields[i].setEditable(false);
          	priceTextFields[i].setMaximumSize( priceTextFields[i].getPreferredSize() );
          	billLines[i].setPriceTextField(priceTextFields[i]);
          	pricePanels[i].add(priceTextFields[i]);
          	
          	amountTextFields[i] = new JTextField(3);
          	amountTextFields[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
          	amountTextFields[i].setDocument(new JTextFieldFilter(JTextFieldFilter.NUMERIC));
          	amountTextFields[i].setMaximumSize( amountTextFields[i].getPreferredSize() );
          	amountTextFields[i].setEditable(false);
          	
          	amountTextFields[i].getDocument().addDocumentListener(new DocumentListener() {
          			
					@Override
					public void removeUpdate(DocumentEvent arg0) {
						// TODO Auto-generated method stub
						try{
							
							amount=Integer.parseInt(amountTextFields[selectedLine].getText());
							billLines[selectedLine].setAmount(amount);
							CalculateTotalCostOfLine();	
							Calculate(rowCounter);
							
						}catch(NumberFormatException e){
							amountTextFields[selectedLine].setText("");
							sumTextFields[selectedLine].setText("");
							billLines[selectedLine].setAmount(0);
							CalculateTotalCostOfLine();
							Calculate(rowCounter);
						}
					}
					
					@Override
					public void insertUpdate(DocumentEvent arg0) {
						
						try{
						// TODO Auto-generated method stub
						amount=Integer.parseInt(amountTextFields[selectedLine].getText());
						billLines[selectedLine].setAmount(amount);
						CalculateTotalCostOfLine();
						Calculate(rowCounter);
						}catch(NumberFormatException e){
							amountTextFields[selectedLine].setText("");
							sumTextFields[selectedLine].setText("");
							billLines[selectedLine].setAmount(0);
							CalculateTotalCostOfLine();
							Calculate(rowCounter);
						}
						
						}
					
					@Override
					public void changedUpdate(DocumentEvent arg0) {
						// TODO Auto-generated method stub
						try{
							amount=Integer.parseInt(amountTextFields[selectedLine].getText());
							Float f=Float.parseFloat(amountTextFields[selectedLine].getText());
							f=billLines[selectedLine].getSpecialPrice()*(f-billLines[selectedLine].getBillPromotionAmount());
							String temp=Float.toString(f);
							if(f>0){
								sumTextFields[selectedLine].setText(temp);
						    }
							billLines[selectedLine].setAmount(amount);
							CalculateTotalCostOfLine();
							Calculate(rowCounter);
						
						}catch(NumberFormatException e){
							amountTextFields[selectedLine].setText("");
							sumTextFields[selectedLine].setText("");
							billLines[selectedLine].setAmount(0);
							CalculateTotalCostOfLine();
							Calculate(rowCounter);
							
					
						}
					}
				});
          
          	billLines[i].setAmountTextField(amountTextFields[i]);
          	pricePanels[i].add(amountTextFields[i]);
          	
          	
          	
          	
          	promotionAmountTextFields[i] = new JTextField(3);
          	promotionAmountTextFields[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
          	promotionAmountTextFields[i].setDocument(new JTextFieldFilter(JTextFieldFilter.NUMERIC));
          	promotionAmountTextFields[i].setMaximumSize( promotionAmountTextFields[i].getPreferredSize() );
          	promotionAmountTextFields[i].setEditable(false);
          	
          	promotionAmountTextFields[i].getDocument().addDocumentListener(promotionDocumentListener);
          
          	////////////////////////System.out.println("outside edit totalpromotion="+totalPromotion);
          	
          	
          
          	billLines[i].setPromotionTextField(promotionAmountTextFields[i]);
          	pricePanels[i].add(promotionAmountTextFields[i] );
          	
          	
          	totalPromotionTextFields[i] = new JTextField(3);
          	totalPromotionTextFields[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
          	totalPromotionTextFields[i].setDocument(new JTextFieldFilter(JTextFieldFilter.NUMERIC));
          	totalPromotionTextFields[i].setMaximumSize( totalPromotionTextFields[i].getPreferredSize() );
          	totalPromotionTextFields[i].setEditable(false);
          	
          	billLines[i].setTotalPromotionTextField(totalPromotionTextFields[i]);
          	
          	
          	pricePanels[i].add(totalPromotionTextFields[i] );
          	
          	sumTextFields[i] = new JTextField(3);
          	sumTextFields[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
          	sumTextFields[i].setEditable(false);
          	sumTextFields[i].setMaximumSize( sumTextFields[i].getPreferredSize() );
          	billLines[i].setSumTextField(sumTextFields[i]);
          	pricePanels[i].add(sumTextFields[i]);
          	addItem(BillAdditionMenu.customerPanel, pricePanels[i], 3, i, 1, 1, GridBagConstraints.WEST,20);
          	
          	
          	payingPanels[i]=new JPanel();
          	payingPanels[i].setLayout(new GridLayout(1,3));
          	
          	creditCheckBoxs[i]=new CustomCheckBox(i);
          	creditCheckBoxs[i].addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						if(creditCheckBoxIsClicked){
							billLines[selectedLine].setCreditCheckBoxStatus(false);
							billLines[selectedLine].getCreditCheckBox().setEnabled(true);
							billLines[selectedLine].getCreditCashTextField().setEditable(true);
							billLines[selectedLine].setCreditCashTextFieldStatus(false);
							billLines[selectedLine].getCashCheckBox().setEnabled(true);
							billLines[selectedLine].setCashCheckBoxStatus(false);
							billLines[selectedLine].getCreditCashTextField().setEnabled(true);
							billLines[selectedLine].getCreditCashTextField().addMouseListener(CustomMouseListener);
							creditCheckBoxIsClicked=false;
						
						Calculate(rowCounter);
						
						}
						else{
							billLines[selectedLine].setCreditCheckBoxStatus(true);
							billLines[selectedLine].setCashCheckBoxStatus(false);
							billLines[selectedLine].getCreditCashTextField().setText("");
							billLines[selectedLine].setCreditCashTextFieldStatus(false);
							billLines[selectedLine].setPayedCash(0);
							billLines[selectedLine].getCreditCashTextField().setEditable(false);
							billLines[selectedLine].getCashCheckBox().setEnabled(false);
							billLines[selectedLine].getCashCheckBox().setSelected(false);
							billLines[selectedLine].getCreditCashTextField().setEnabled(false);
							billLines[selectedLine].getCreditCashTextField().removeMouseListener(CustomMouseListener);
							creditCheckBoxIsClicked=true;
							
							Calculate(rowCounter);
							
						}
						
						Calculate(rowCounter);
						
					}

					
				} );
        	billLines[i].setCreditCheckBox(creditCheckBoxs[i]);
          	billLines[i].getCreditCheckBox().setEnabled(false);
          	payingPanels[i].add(billLines[i].getCreditCheckBox());
          	
          	cashCheckBoxs[i]=new CustomCheckBox(i);
          	cashCheckBoxs[i].addActionListener(new ActionListener() {
          
					@Override
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						////////////////////System.out.println("CashCehckBoxClicked!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
						
						if(cashCheckBoxIsClicked){
							billLines[selectedRadioButton].setCashCheckBoxStatus(false);
							billLines[selectedRadioButton].getCashCheckBox().setEnabled(true);
							billLines[selectedRadioButton].getCreditCashTextField().setEditable(true);
							billLines[selectedRadioButton].setCreditCashTextFieldStatus(false);
							billLines[selectedRadioButton].getCreditCheckBox().setEnabled(true);
							billLines[selectedRadioButton].setCreditCheckBoxStatus(false);
							billLines[selectedRadioButton].getCreditCashTextField().setEnabled(true);
							billLines[selectedRadioButton].getCreditCashTextField().addMouseListener(CustomMouseListener);
							cashCheckBoxIsClicked=false;
							
							if(!billLines[selectedRadioButton].getCreditCashTextField().getText().equals("") || billLines[selectedRadioButton].getCreditCashTextField().getText().equals("0.0")){
								billLines[selectedRadioButton].setBillCash(Float.parseFloat(billLines[selectedRadioButton].getCreditCashTextField().getText()));
							}
							
							Calculate(rowCounter);
							
							}
							else{
								billLines[selectedRadioButton].setCashCheckBoxStatus(true);
								billLines[selectedRadioButton].setCreditCheckBoxStatus(false);
								billLines[selectedRadioButton].getCreditCashTextField().setText("");
								billLines[selectedRadioButton].setCreditCashTextFieldStatus(false);
								billLines[selectedRadioButton].setPayedCash(0);
								billLines[selectedRadioButton].getCreditCashTextField().setEditable(false);
								billLines[selectedRadioButton].getCreditCashTextField().setEnabled(false);
								billLines[selectedRadioButton].getCreditCheckBox().setEnabled(false);
								billLines[selectedRadioButton].getCreditCheckBox().setSelected(false);
								billLines[selectedRadioButton].getCreditCashTextField().removeMouseListener(CustomMouseListener);
								billLines[selectedRadioButton].setPayedCash(billLines[selectedRadioButton].getBillTotalCost());
								//////////////System.out.println("bill cash set to"+billLines[selectedRadioButton].getPayedCash());
								cashCheckBoxIsClicked=true;
								
								
								Calculate(rowCounter);
							}
						
					}

					
				} );
        	billLines[i].setCashCheckBox(cashCheckBoxs[i]);
        	billLines[i].getCashCheckBox().setEnabled(false);
          	payingPanels[i].add(billLines[i].getCashCheckBox());
          	
          	creditCashTextFields[i]=new JTextField(3);
          	creditCashTextFields[i].setMaximumSize( sumTextFields[i].getPreferredSize() );
          	creditCashTextFields[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
          	creditCashTextFields[i].setDocument(new JTextFieldFilter(JTextFieldFilter.NUMERIC));
          	
          	creditCashTextFields[i].removeMouseListener(CustomMouseListener);
          	creditCashTextFields[i].setEditable(false);
          	creditCashTextFields[i].setEnabled(false);
          	
          	creditCashTextFields[i].getDocument().addDocumentListener(new DocumentListener() {
      			
				@Override
				public void removeUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub
					try{
						billLines[selectedLine].setCreditCashTextFieldStatus(true);
						if(!billLines[selectedLine].getCreditCashTextField().equals("")){
							Float f=Float.parseFloat(billLines[selectedLine].getCreditCashTextField().getText());
							billLines[selectedLine].setPayedCash(f);
						}
						Calculate(rowCounter);
					
					}catch(NumberFormatException e){
						billLines[selectedLine].getCreditCashTextField().setText("");
						billLines[selectedLine].setPayedCash(0);
						Calculate(rowCounter);
						
				
					}
				}
				
				@Override
				public void insertUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub
					billLines[selectedLine].setCreditCashTextFieldStatus(true);
					if(!billLines[selectedLine].getCreditCashTextField().getText().equals("")){
						Float f=Float.parseFloat(billLines[selectedLine].getCreditCashTextField().getText());
						billLines[selectedLine].setPayedCash(f);
					}
					Calculate(rowCounter);
					
					}
				
				@Override
				public void changedUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub
					try{
						billLines[selectedLine].setCreditCashTextFieldStatus(true);
						if(!billLines[selectedLine].getCreditCashTextField().equals("")){
							Float f=Float.parseFloat(billLines[selectedLine].getCreditCashTextField().getText());
							billLines[selectedLine].setPayedCash(f);
						}
						Calculate(rowCounter);
					
					}catch(NumberFormatException e){
						billLines[selectedLine].getCreditCashTextField().setText("");
						billLines[selectedLine].setPayedCash(0);
						Calculate(rowCounter);
						
				
					}
				}
			});
      	
          	
          	
          	billLines[i].setCreditCashTextField(creditCashTextFields[i]);
          	
          	payingPanels[i].add(billLines[i].getCreditCashTextField());
          	
          	/*c.gridx=3;
          	c.gridy=i;
          	c.gridwidth = 1;
              c.gridheight = 1;
              c.ipady=20;
              c.anchor = GridBagConstraints.FIRST_LINE_START;
              c.fill = GridBagConstraints.BOTH;
              c.weightx =1;  // **** comment this line out to see effect ****
              c.weighty =0;  // **** comment this line out to see effect *****/
          	addItem(BillAdditionMenu.customerPanel,payingPanels[i], 4, i, 1,1, GridBagConstraints.WEST,20);
          	
          	
          	
          	
          	
          	
          
          	//customerPanel.add(Box.createVerticalStrut(15));
          	//initialPosition=initialPosition+40;
          }
          if(rowCounter<9 && rowCounter!=0){ 
        	  for(int m=rowCounter-1;m<15;m++){
        		  addItem(BillAdditionMenu.customerPanel,new JLabel(), 0, m, 1,1, GridBagConstraints.WEST,20);
        	  }
          }
          
          if(rowCounter==0){
        	  for(int m=0;m<15;m++){
        		  addItem(BillAdditionMenu.customerPanel,new JLabel(), 0, m, 1,1, GridBagConstraints.WEST,20);
        	  }  
          }
          
          if(tempRowCounter>0){
        	  for(int b=0;b<tempRowCounter;b++){
        		  billLines[b].getBillProductComboBox().setEnabled(false);
        	  }
        	  
          }
          
          
          
          for(int g=0;g<BillAdditionMenu.destinationsInRoute.length;g++){
        	  //////////System.out.println("Customer "+g+"= "+BillAdditionMenu.destinationsInRoute[g]);
          }
          
          
          
          
         
         
          fillBillSpecialPage();
          
          
          if(tempRowCounter>0){
      		editRadioButtons[selectedRadioButton].setSelected(true);
      		saveButtons[selectedRadioButton].setEnabled(true);
      		editButtons[selectedRadioButton].setEnabled(true);
      		deleteButtons[selectedRadioButton].setEnabled(true);
      		plusButtons[selectedRadioButton].setEnabled(true);
      		minusButtons[selectedRadioButton].setEnabled(true);
      		editRadioButtons[selectedRadioButton].setSelected(true);
        	billLines[selectedRadioButton].getBillProductComboBox().setEnabled(true);
      	}
          
         
         
          
          
          //JScrollPane scrollPane = new JScrollPane();
          //scrollPane.add(customerPanel);
          //jsp.revalidate();
          //contentPane.add(jsp);
          contentPane.revalidate();
		
	}
	
	private static void fillBillSpecialPage() {
		
		
		UpdatePromotions();
		
		
        
        
        for(int g=0;g<BillAdditionMenu.destinationsInRoute.length;g++){
      	String unique=BillAdditionMenu.destinationsInRoute[g];
      	int uniqueCounter=0;
      	for(int k=g;k<BillAdditionMenu.destinationsInRoute.length;k++){
          	if(unique.equals(BillAdditionMenu.destinationsInRoute[k])){
          		uniqueCounter++;
          	}
          	
            }
      	  ////////System.out.println("uniqueId= "+unique+" how many= "+uniqueCounter+" customerCode= "+billLines[g].getCustomerCode());
      	  if(tempRowCounter>0){
      		  FillBillDataAction.fillBillSpecialPageData(billLines,BillAdditionMenu.getDate(), products, promotions, customers, uniqueCounter,g,billLines[g].getCustomerCode());
      		  //FillBillDataAction.fillBillSpecialPageData(billLines,BillAdditionMenu.getDate(), products, promotions, customers, uniqueCounter,g,billLines[g].getCustomerCode());
      		  
      	  }
      	  g=g+uniqueCounter-1;
        }
        
        
        
        
       
        
        
        int duplicateCounter=0;	
        for(int g=selectedRadioButton;g>0;g--){
          	
        	if(Integer.parseInt(BillAdditionMenu.destinationsInRoute[g])==billLines[selectedRadioButton].getCustomerId()){
        		duplicateCounter++;
        	}
          	
          	 
            }
        
            System.out.println("I have "+duplicateCounter+" duplicate row before this row");
        	
        	
        	
        	
        	if(deleteFlag){
        	
        		if(tempRowCounter>0 && selectedRadioButton!=0){	
        					FillBillDataAction.fillBillPageData(billLines[selectedRadioButton], BillAdditionMenu.getDate(),products,promotions,customers,duplicateCounter);

        		}
        	}
        	
        	
        	if(tempRowCounter>0 && selectedRadioButton==0){
        		FillBillDataAction.fillBillPageData(billLines[selectedRadioButton], BillAdditionMenu.getDate(),products,promotions,customers,0);
        	}
       
       
        
        deleteFlag=true;
        	
		
		
		
	
	}

	private static void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align,int ipady) {
        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.ipady=ipady;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets = new Insets(3,0,3,0);
        gc.anchor = align;
        gc.fill = GridBagConstraints.HORIZONTAL;
        p.add(c, gc);
      }
	
	public static MouseListener CustomMouseListener = new MouseListener() {
		

		@Override
		public void mouseClicked(MouseEvent arg0) {
			if(mouseClicked){
				creditCashTextFields[selectedRadioButton].setEditable(true);
				billLines[selectedRadioButton].setCreditCashTextFieldStatus(false);
				cashCheckBoxs[selectedRadioButton].setEnabled(true);
				billLines[selectedRadioButton].setCashCheckBoxStatus(false);
				mouseClicked=false;
			}
			else{
				creditCashTextFields[selectedRadioButton].setEditable(true);
				creditCashTextFields[selectedRadioButton].setEnabled(true);
				mouseClicked=true;
				//billLines[selectedLine].setCreditCashTextFieldStatus(true);
			}
			
		}

		@Override
		public void mouseEntered(
				MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(
				MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(
				MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
	};
	
	public static void Calculate(int rowCounter){
		////////////////////////System.out.println("Calculating....");	
		
		
		totalCash=0;
		
		for(int k=0;k<rowCounter;k++){
			if(billLines[k].getCashCheckBoxStatus()){
				//////////////////////////System.out.println("Checkbox Checked and total cash calculated");
				totalCash=totalCash+billLines[k].getBillTotalCost();
				//////////////////////System.out.println("totalCash="+totalCash);
				billLines[k].setBillCash(billLines[k].getBillTotalCost());
			}
			if(billLines[k].getCreditCashTextFieldStatus()){
				totalCash=totalCash+billLines[k].getPayedCash();
				billLines[k].setBillCash(billLines[k].getPayedCash());
			}
		}
		////////////////////////System.out.println("totalCash="+totalCash);
		BillAdditionMenu.totalCashLabel.setText(new DecimalFormat("##.##").format(totalCash));
		
		
		total=0;
		for(int k=0;k<rowCounter;k++){
			////////////////////////System.out.println("total cost= "+billLines[k].getBillTotalCost()+"   total promotion amount in calculate="+billLines[k].getBillPromotionAmount()+"   special price="+billLines[k].getSpecialPrice());
			total=total+billLines[k].getBillTotalCost();
		}
		BillAdditionMenu.totalLabel.setText(new DecimalFormat("##.##").format(total));
		
		
		
		/*totalCredit=0;
		
		
		for(int k=0;k<rowCounter;k++){
			if(billLines[k].getCreditCheckBoxStatus()){
				totalCredit=totalCredit+billLines[k].getBillTotalCost();
				//////////////////////System.out.println("totalCredit="+totalCredit);
				billLines[k].setBillCredit(billLines[k].getBillTotalCost());
			}
			if(billLines[k].getCreditCashTextFieldStatus()){
				totalCredit=totalCredit+billLines[k].getBillTotalCost()-billLines[k].getPayedCash();
				billLines[k].setBillCredit(billLines[k].getBillTotalCost()-billLines[k].getPayedCash());
			}
		}
		System.out.println("totalCredit="+totalCredit);*/
		
		BillAdditionMenu.totalCreditLabel.setText(new DecimalFormat("##.##").format(total-totalCash));
		
		
	}
	
	
	public ActionListener comboBoxListener= new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			
		}
	};
	
	
	public static DocumentListener promotionDocumentListener = new DocumentListener() {
		
		@Override
		public void removeUpdate(DocumentEvent arg0) {
			CalculateNewPromotion();
		}
		
		@Override
		public void insertUpdate(DocumentEvent arg0) {
			CalculateNewPromotion();
			}
	
		@Override
		public void changedUpdate(DocumentEvent arg0) {
			CalculateNewPromotion();
		}
	};


	protected static void CalculateTotalCostOfLine() {
		
		if(!amountTextFields[selectedRadioButton].getText().equals("")){
			Float f=Float.parseFloat(amountTextFields[selectedRadioButton].getText());
			f=billLines[selectedRadioButton].getSpecialPrice()*(f-billLines[selectedRadioButton].getBillPromotionAmount());
			if(f>0)
			sumTextFields[selectedRadioButton].setText(new DecimalFormat("##.##").format(f));
			billLines[selectedRadioButton].setBillTotalCost(Float.parseFloat(new DecimalFormat("##.##").format(f)));
		}
		else{
			Float f=(float) 0;
			if(f>0)
			sumTextFields[selectedRadioButton].setText(new DecimalFormat("##.##").format(f));
			billLines[selectedRadioButton].setBillTotalCost(Float.parseFloat(new DecimalFormat("##.##").format(f)));
		}
	}
	
	
	public static void CalculateNewPromotion(){
		
		/*////////////////////System.out.println("LineNo="+billLines[selectedRadioButton].getbillLineid());
		totalPromotion=billLines[selectedRadioButton].getBillPromotionUnit();
		////////////////////System.out.println("totalPromotion="+totalPromotion);
		////////////////////System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		////////////////////////System.out.println("Total promotion Unit="+tempTotalPromotion);
		 * 
*/		
		try{
			//////////////////////System.out.println("selectedRadioButton="+selectedRadioButton);
			promotionAmount=0;
			totalPromotion=billLines[selectedRadioButton].getBillModifyingPromotionUnit();
			int firstPromotionAmount=billLines[selectedRadioButton].getBillPromotionAmount();
			tempTotalPromotion=0;
			
			////////////////////System.out.println("totalPromotion="+totalPromotion);
			if(!promotionAmountTextFields[selectedRadioButton].getText().equals("")){
				promotionAmount=Integer.parseInt(promotionAmountTextFields[selectedRadioButton].getText());
				
				
			}
			else{
				////////////////////System.out.println("in exception and before exception promotionAmount="+promotionAmount);
				promotionAmount=0;
	
				
			}
			////////////////////System.out.println("promotionAmount="+promotionAmount);
			
			if(promotionAmount!=billLines[selectedRadioButton].getBillPromotionAmount()){
				
				
					
					////////////////////System.out.println("promotionAmount condition sağlandı!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!=  "+promotionAmount);
					tempTotalPromotion=totalPromotion;
				
					if(promotionAmount>=billLines[selectedRadioButton].getBillModifyingPromotionAmount()+billLines[selectedRadioButton].getBillModifyingPromotionUnit()){
						promotionAmount=billLines[selectedRadioButton].getBillModifyingPromotionAmount()+billLines[selectedRadioButton].getBillModifyingPromotionUnit();
					}
			
					
					else if(promotionAmount>=billLines[selectedRadioButton].getAmount()){
						promotionAmount=billLines[selectedRadioButton].getAmount();
						billLines[selectedRadioButton].getSumTextField().setText("0");
					}
				
					else if(promotionAmount>0 && promotionAmount<=tempTotalPromotion){
						tempTotalPromotion=tempTotalPromotion-promotionAmount;
					}
			
				
				
				
				
				billLines[selectedRadioButton].setBillModifyingPromotionAmount(promotionAmount);
				billLines[selectedRadioButton].setBillModifyingPromotionUnit(tempTotalPromotion);
				
				////////////////System.out.println("***************************************************************************");
				////////////////System.out.println("***************************************************************************");
				////////////////System.out.println();
				////////////////System.out.println("totalPromotion="+totalPromotion);
				////////////////System.out.println("billLines[selectedRadioButton].getBillPromotionAmount() (exPromotionAmount)="+billLines[selectedRadioButton].getBillPromotionAmount());
				////////////////System.out.println("billLines[selectedRadioButton].getBillModifyingPromotionAmount()(newPromotionAmount)="+billLines[selectedRadioButton].getBillModifyingPromotionAmount());
				/*//////////////System.out.println("newTotalPromotion="+(totalPromotion+
						(billLines[selectedRadioButton].getBillPromotionAmount()-billLines[selectedRadioButton].getBillModifyingPromotionAmount())));*/
				////////////////System.out.println();
				////////////////System.out.println("***************************************************************************");
				////////////////System.out.println("***************************************************************************");
				
				
				
				
				//billLines[selectedRadioButton].getTotalPromotionTextField().setText(Integer.toString(billLines[selectedRadioButton].getBillModifyingPromotionUnit()));
				billLines[selectedRadioButton].setBillModifyingPromotionUnit(totalPromotion+
									(billLines[selectedRadioButton].getBillPromotionAmount()-billLines[selectedRadioButton].getBillModifyingPromotionAmount()));
		
				billLines[selectedRadioButton].getTotalPromotionTextField().setText(Integer.toString(billLines[selectedRadioButton].getBillModifyingPromotionUnit()));
				if(billLines[selectedRadioButton].getBillModifyingPromotionUnit()==0){
					billLines[selectedRadioButton].getSumTextField().setText("0");
				}
				
				billLines[selectedRadioButton].setBillPromotionAmount(promotionAmount);
				billLines[selectedRadioButton].setBillModifyingPromotionAmount(promotionAmount);
			}
			
			
			
			
			CalculateTotalCostOfLine();
			Calculate(tempRowCounter);
			
		}catch(NumberFormatException e){
			
		}

		
		
	}

	public static void UpdatePromotions(){
		
		Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/mydb";
        String user = "root";
        String password = "qweasdzxc";
        int promotionCounter=0;
        int count=0;
		try {
			
			 con = (Connection) DriverManager.getConnection(url, user, password);
	         st = (Statement) con.createStatement();
			
			 rs=st.executeQuery("SELECT * from promotion");
			 while(rs.next()){
		        	promotionCounter++;
		        }
		        
			 promotions=new Promotion[promotionCounter];
			 if(promotionCounter>0){
			 
		       
		        promotions[0]=new Promotion();
		        rs.first();
		        promotions[0].setPromotionId(rs.getInt("promotion_id"));
		        promotions[0].setPromotionProductId(rs.getInt("promotion_product_id"));
		        promotions[0].setPromotionCustomerId(rs.getInt("promotion_customer_id"));
		        promotions[0].setPromotionUnit(rs.getInt("promotion_unit"));
		        
		        
		        //productArray[0].SetLongProductName(rs.getString("product_manufacturer")+" "+rs.getString("product_name"));
		      
		        count=1;
		        
		        while(rs.next()){
		        	promotions[count]=new Promotion();
		        	promotions[count].setPromotionId(rs.getInt("promotion_id"));
		        	promotions[count].setPromotionProductId(rs.getInt("promotion_product_id"));
		            promotions[count].setPromotionCustomerId(rs.getInt("promotion_customer_id"));
		            promotions[count].setPromotionUnit(rs.getInt("promotion_unit"));
		        	//productArray[count].SetLongProductName(rs.getString("product_manufacturer")+" "+rs.getString("product_name"));
		        
		        	count++;
		        }
		        
			 }
		        
			con.close();	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
       
		
		
	}
	
	
	
	public static void enableLine(){
		
		for(int g=0;g<billLines.length;g++){
			billLines[g].getPriceTextField().setEditable(false);
			billLines[g].getAmountTextField().setEditable(false);
			billLines[g].getPromotionTextField().setEditable(false);
			billLines[g].getTotalPromotionTextField().setEditable(false);
			billLines[g].getSumTextField().setEditable(false);
			billLines[g].getCreditCheckBox().setEnabled(false);
			billLines[g].getCashCheckBox().setEnabled(false);
			billLines[g].getCreditCashTextField().setEditable(false);	
		}
		
		
		billLines[selectedRadioButton].getPriceTextField().setEnabled(true);
		billLines[selectedRadioButton].getPriceTextField().setEditable(true);
		
		billLines[selectedRadioButton].getAmountTextField().setEnabled(true);
		billLines[selectedRadioButton].getAmountTextField().setEditable(true);
		
		billLines[selectedRadioButton].getPromotionTextField().setEnabled(true);
		billLines[selectedRadioButton].getPromotionTextField().setEditable(true);
		
		billLines[selectedRadioButton].getTotalPromotionTextField().setEnabled(true);
		billLines[selectedRadioButton].getTotalPromotionTextField().setEditable(true);
		
		billLines[selectedRadioButton].getSumTextField().setEnabled(true);
		billLines[selectedRadioButton].getSumTextField().setEditable(true);
		
		
		billLines[selectedRadioButton].getCreditCheckBox().setEnabled(true);
		billLines[selectedRadioButton].getCashCheckBox().setEnabled(true);
		billLines[selectedRadioButton].getCreditCashTextField().setEnabled(true);	
		
		
		
		
		
	}
		
	
	
	

}